import customtkinter as ctk
from customtkinter import CTkButton, CTkFrame
import webbrowser

class TempMailApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Cursor重置工具")
        self.root.geometry("600x400")
        
        # 设置主题
        ctk.set_appearance_mode("dark")
        ctk.set_default_color_theme("blue")
        
        # 创建主容器
        self.main_container = CTkFrame(root)
        self.main_container.pack(padx=20, pady=20, fill="both", expand=True)
        
        # 标题
        self.title_label = ctk.CTkLabel(
            self.main_container,
            text="Cursor 重置工具",
            font=("Arial", 24, "bold")
        )
        self.title_label.pack(pady=(0, 20))
        
        # 重置按钮
        self.reset_btn = CTkButton(
            self.main_container,
            text="重置机器码",
            width=200,
            height=40,
            font=("Arial", 14),
            command=self.reset_machine_code
        )
        self.reset_btn.pack(pady=20)
        
        # 临时邮箱链接标题
        self.email_label = ctk.CTkLabel(
            self.main_container,
            text="推荐临时邮箱网站",
            font=("Arial", 16, "bold")
        )
        self.email_label.pack(pady=(20, 10))
        
        # 临时邮箱链接
        self.email_sites = [
            ("TempMail", "https://temp-mail.org"),
            ("Guerrilla Mail", "https://www.guerrillamail.com"),
            ("10分钟邮箱", "https://email10min.com/zh"),
            ("临时电子邮件", "https://www.temporary-email.org/zh"),
            ("一次性临时邮箱", "https://tempmail.plus/zh/#!"),
            ("Moakt Email", "https://www.moakt.com/zh/")
        ]
        
        # 创建按钮容器（使用网格布局）- 移到最后
        self.button_container = CTkFrame(self.main_container)
        self.button_container.pack(side="bottom", fill="both", expand=True)
        
        # 使用网格布局来放置按钮
        for i, (name, url) in enumerate(self.email_sites):
            row = i // 2  # 计算行号
            col = i % 2   # 计算列号
            btn = CTkButton(
                self.button_container,
                text=name,
                width=180,  # 稍微减小按钮宽度
                height=35,
                font=("Arial", 13),
                command=lambda u=url: webbrowser.open(u)
            )
            btn.grid(row=row, column=col, pady=5, padx=10)
            
        # 配置网格列的权重，使按钮均匀分布
        self.button_container.grid_columnconfigure(0, weight=1)
        self.button_container.grid_columnconfigure(1, weight=1)

    def reset_machine_code(self):
        import sys
        import os
        
        # 获取程序运行路径
        if getattr(sys, 'frozen', False):
            # 如果是打包后的程序
            application_path = os.path.dirname(sys.executable)
        else:
            # 如果是开发环境
            application_path = os.path.dirname(os.path.abspath(__file__))
            
        # 将 ResetMachineCode.py 所在目录添加到系统路径
        sys.path.append(application_path)
        
        # 现在导入 ResetMachineCode
        try:
            from ResetMachineCode import reset_machine_code
            reset_machine_code()
        except Exception as e:
            import tkinter.messagebox as messagebox
            messagebox.showerror("错误", f"重置失败：{str(e)}")

if __name__ == "__main__":
    root = ctk.CTk()
    app = TempMailApp(root)
    root.mainloop()
